package com.example.mvvmtest.views;

import android.app.ActivityManager;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.example.mvvmtest.R;
import com.example.mvvmtest.databinding.MainBinding;
import com.example.mvvmtest.model.bean.User2;
import com.example.mvvmtest.model.bean.UserBean;
import com.example.mvvmtest.service.MyService;
import com.example.mvvmtest.view_model.MVVMDataViewModel;
import com.example.mvvmtest.view_model.MainOnClick;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import fm.jiecao.jcvideoplayer_lib.JCVideoPlayer;
import fm.jiecao.jcvideoplayer_lib.JCVideoPlayerStandard;

/**
 * View层
 */
public class MVVMActivity extends AppCompatActivity {

    private MVVMDataViewModel userViewModel;
    private MainBinding binding;
    private TextView tvData;
    private UserBean userBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_mvvmpattern);

        userBean = new UserBean("Clear", "Liang", false);
        binding.setUser(userBean);
        userViewModel = new MVVMDataViewModel();
        binding.setUserViewModel(userViewModel);
        binding.setMyOnclick(new MainOnClick());
        binding.setHandlers(this);
        binding.setImageUrl("http://a1.att.hudong.com/05/00/01300000194285122188000535877.jpg");
        tvData = binding.tvData;

        User2 user2 = new User2();
        user2.parents.put("mom", "1");
        user2.parents.put("day", "2");

        //initVideo();
//
//        DownLoadTask task = new DownLoadTask();
//        task.execute();
        Intent myServiceIntent = new Intent(MVVMActivity.this, MyService.class);
        this.startService(myServiceIntent);

    }

    public void onClickShowToastName(View view) {
        Toast.makeText(this, tvData.getText().toString(), Toast.LENGTH_SHORT).show();
    }

    public void onClickLoadData(View view) {
        userViewModel.loadUserData();
    }

    private int flag = 0;

    public void onChangeText(View view) {
        jcVideoPlayerStandard.setRotation(flag);
        flag = flag == 0 ? 180 : 0;

//        TextView textView = (TextView) view;
//        Toast.makeText(this, String.valueOf(textView.getText()), Toast.LENGTH_SHORT).show();
//        userBean.setFirstName("Clear1");
//        userBean.setLastName("Liang1");
//        userBean.setStudent(!userBean.isStudent());
    }

    private void test() {
        List<String> linkedList = new LinkedList<>();
        List<String> arrayList = new ArrayList<>();

        linkedList.add(0, "");
        arrayList.add(0, "");

        ActivityManager activityManager = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();

        for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {

        }

    }

    private JCVideoPlayerStandard jcVideoPlayerStandard;

    private void initVideo() {
        jcVideoPlayerStandard = (JCVideoPlayerStandard) findViewById(R.id.videoplayer);
        jcVideoPlayerStandard.setUp("http://2449.vod.myqcloud.com/2449_22ca37a6ea9011e5acaaf51d105342e3.f20.mp4", JCVideoPlayerStandard.SCREEN_LAYOUT_NORMAL, "嫂子闭眼睛");
    }

    @Override
    public void onBackPressed() {
        if (JCVideoPlayer.backPress()) {
            return;
        }
        super.onBackPressed();
    }

    @Override
    protected void onPause() {
        super.onPause();
        JCVideoPlayer.releaseAllVideos();
    }

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
        }
    };
}
